#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <iomanip.h>
#include <stdlib.h>

struct list
{
  int x;
  list* next;
};

list* head = NULL;
list* ptr = NULL;
list* rear = NULL;

void make_list(int a);
void print_list(list* s);
//void delete_element(list* s, int a);
void delete_list(list* s);

void main(void)
{
  randomize();
  clrscr();
  int n;
  //while (n){
  cout<< "How many elemants do you want to make?  ";
  cin>> n;
  if (!isdigit(n))
  {
    cout<< "Please input digit.  ";
  }

  list* s = new list;
  for (int i=0; i<n; i++)
  {
    make_list(random(100) - 50);
  }
  s = head;
  print_list(s);

  list* p = new list;
  list* q = new list;
  head = NULL;
  list* temp = s;
  int count = 0;
  while (temp)
  {
    if (temp->x < 0)
    {
      make_list(temp->x);
      //delete_element(p, count);
    }
    temp = temp->next;
    count++;
  }
  p = head;

  head = NULL;
  temp = s;
  count = 0;
  while (temp)
  {
    if (temp->x > 0)
    {
      make_list(temp->x);
      //delete_element(p, count);
    }
    temp = temp->next;
    count++;
  }
  q = head;

  cout<< "New lists :"<< endl;
  print_list(p);
  print_list(q);
  delete_list(s);
  delete_list(p);
  delete_list(q);//}
  getch();
}

void make_list(int a)
{
  ptr = new list;
  ptr->x = a;
  if (!head)
  {
    head = ptr;
  }
  else
  {
    rear->next = ptr;
  }
  rear = ptr;
  rear->next = NULL;
}

void print_list(list* s)
{
  ptr = s;
  while (ptr)
  {
    cout<< setw(4)<< ptr->x;
    ptr = ptr->next;
  }
  cout<< endl;
}

/*void delete_element(list* s, int a)
{
  ptr = s;
  list* temp;

  for (int i=0; i<a-1; i++)
  {
    ptr = ptr->next;
  }
  if (a == 0)
  {
    s = ptr->next;
    temp = ptr;
    delete temp;
  }
  else
  {
    temp = ptr->next;
    ptr->next = temp->next;
    delete temp;
  }
}
*/
void delete_list(list* s)
{
  list* temp;
  ptr = s;
  while (ptr)
  {
    temp = ptr;
    ptr = ptr->next;
    delete temp;
  }
}



/*  while (i != a-1)
  {
    ptr = ptr->next;
    i++;
  }
*/

